package com.hnqcgc.fz.db;

import java.sql.Connection;
import java.sql.SQLException;

public class MyTransaction {
    private Connection conn;

    public MyTransaction(boolean autoCommit){
        try {

            conn= com.hnqcgc.fz.db.MyConnection.getInstance().getConn();
            this.conn.setAutoCommit(autoCommit);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //获取数据库连接
    public Connection getConnection(){
        return this.conn;
    }
    //提交
    public void commit(){
        try {
            this.conn.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //回滚
    public void rollback(){
        try {
            this.conn.rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //关闭数据库连接方法
    public void closeConn(){
        try {
            if(conn!=null&&conn.isClosed()){
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
